<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="time"></div>
    <script>
        //写一个函数，格式化时间 formatDate(),参数可以是字符串，也可以是时间对象。效果： 2021-10-05 15：15：09 星期几
        //var div = document.querySelector('div');
        var a = setInterval(function formData() {            //获取当前的年月日时间
            var nowTime = new Date();
            var year = nowTime.getFullYear();
            var month1 = nowTime.getMonth() + 1;
            //三元运算符，如果月份小于10则在前面补0，如果没有小于10则输出月份
            var month2 = month1 < 10 ? '0' + month1 : month1;
            //获取日期
            var day1 = nowTime.getDate();
            var day2 = day1 < 10 ? '0' + day1 : day1
            var week1 = nowTime.getDay();
            var arr = ["星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
            var week2 = arr[week1];
            var h = nowTime.getHours();
            var h2 = h < 10 ? '0' + h : h;
            var m = nowTime.getMinutes();
            var m2 = m < 10 ? '0' + m : m;
            var s = nowTime.getSeconds();
            var s2 = s < 10 ? '0' + s : s;
            var time = year + "年" + month2 + "月" + day2 + "日" + h2 + "时" + m2 + "分" + s2 + "秒";
            document.getElementById('time').innerHTML = time;
        },1000 )
        //setInterval(formData, 1000);
    </script>
</body>

</html>